package org.example.petsystem.controller;

import org.example.petsystem.entity.ResultBody;
import org.example.petsystem.service.EditInfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/EditInformation")
@CrossOrigin(origins="*")
public class EditInfController {
    @Autowired
    private EditInfService editInfService;
    @RequestMapping("/EditMessage")
    public ResultBody EditMessage(@RequestBody Map<String, String> map) {
        // 从请求参数中获取数据
        if (map.get("name") == null || map.get("adminname") == null || map.get("adminsex") == null || map.get("adminnumber") == null  || map.get("adminemail") == null || map.get("birthday") == null||map.get("avatar_Url")==null) {
            return new ResultBody(404, "参数错误", false);
        }
        String name = map.get("name"); // 用户姓名
        String username =  map.get("adminname");//昵称
        String sex = map.get("adminsex");//用户性别
        String number = map.get("adminnumber");//用户电话号码
        String email = map.get("adminemail");//用户邮箱
        String birthday = map.get("birthday");//生日
        String avatar_Url = map.get("avatar_Url");//用户图片
        // 判断用户是否存在
        if (editInfService.findMessage(number) ==  0) {
            return new ResultBody(400, "用户不存在", false);
        } else {
            // 更新用户信息
            if (editInfService.editInformation(name,username, sex,number,email,birthday,avatar_Url)== 1) {
                return new ResultBody(200, "更新成功",map);
            }
            return new ResultBody(400, "更新失败", false);
        }
    }
    @RequestMapping("/lookMessage")
    public ResultBody lookMessage(@RequestBody Map<String, String> map) {
        if(map.get("id") == null) {
            return new ResultBody(404,"参数错误",false);
        }
        String id = map.get("id");
        return editInfService.UserMessage(id);
    }
}